/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;

/**
 *
 * @author Alan Alves Oliveira
 */
public class UtilidadeSQL {

    public static final void verificaNulo(Object valor, int sqlType, PreparedStatement pst, int indiceParametro) throws SQLException {
        if (valor == null)
            pst.setNull(indiceParametro, sqlType);
        else {
            switch (sqlType) {
                case Types.VARCHAR:
                    String s = (String) valor;
                    if (s.trim().equals(""))
                        pst.setNull(indiceParametro, sqlType);
                    else
                        pst.setString(indiceParametro, s);
                    break;
                case Types.INTEGER:
                    int i = (Integer) valor;
                    if (i == 0)
                        pst.setNull(indiceParametro, sqlType);
                    else
                        pst.setInt(indiceParametro, i);
                    break;
                case Types.FLOAT:
                    float f = (Float) valor;
                    if (f == 0.0)
                        pst.setNull(indiceParametro, sqlType);
                    else
                        pst.setFloat(indiceParametro, f);
                    break;
                case Types.DOUBLE:
                    double d = (Double) valor;
                    if (d == 0.0)
                        pst.setNull(indiceParametro, sqlType);
                    else
                        pst.setDouble(indiceParametro, d);
                    break;
                case Types.BIGINT:
                    long l = (Long) valor;
                    if (l == 0)
                        pst.setNull(indiceParametro, sqlType);
                    else
                        pst.setLong(indiceParametro, l);
                    break;
            }
        }
    }

    public static final String verificaNulo(Object valor, int sqlType) {
        String ret = "null";
        if (valor != null) {
            switch (sqlType) {
                case Types.VARCHAR:
                    String s = (String) valor;
                    if (!s.trim().equals(""))
                        ret = "'" + s.trim() + "'";
                    break;
                case Types.INTEGER:
                    int i = (Integer) valor;
                    if (i == 0)
                        ret = "null";
                    else
                        ret = String.valueOf(i);
                    break;
                case Types.FLOAT:
                    float f = (Float) valor;
                    if (f == 0.0)
                        ret = "null";
                    else
                        ret = String.valueOf(f);
                    break;
                case Types.DOUBLE:
                    double d = (Double) valor;
                    if (d == 0.0)
                        ret = "null";
                    else
                        ret = String.valueOf(d);
                    break;
                case Types.BIGINT:
                    long l = (Long) valor;
                    if (l == 0)
                        ret = "null";
                    else
                        ret = String.valueOf(l);
                    break;
                default:
                    ret = valor.toString();
            }
        }
        return ret;
    }
    
           
        
    }
    
